﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.UserCheckDetailModel;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class UserCheckDetailController : Controller
    {
        readonly IUserCheckDetailServices _userCheckDetail;
        readonly IWorkerTypeInfoServices _workerTypeInfo;
        readonly IUserCheckInfoService _userCheckInfo;
        readonly Config _config;
        readonly IUserServices _user;
        public UserCheckDetailController(IUserCheckDetailServices userCheckDetail, 
            IWorkerTypeInfoServices workerTypeInfo,
            IUserCheckInfoService userCheckInfo,
            Config config,
            IUserServices user) 
        {
            _userCheckDetail = userCheckDetail;
            _workerTypeInfo = workerTypeInfo;
            _userCheckInfo = userCheckInfo;
            _config = config;
            _user = user;
        }

        [HttpPost, Route("Add")]
        public ApiResult Add([FromHeader(Name = "Authorization")] string token, [FromBody] AddUserCheckDetailDto data) 
        {
            if (data == null || !data.WorkerTypeId.NumberGtZero()) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            if (data.BelongsDate > DateTime.Now.AddDays(1) || data.BelongsDate <= _config.MinDate)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            if (!_user.GetUserIdByToken(token.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            if (!_workerTypeInfo.ExistById(data.WorkerTypeId)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_ID_NOT_EXIST);
            }

            data.UserCheckInfoId = _userCheckInfo.GetIdByUserIdAndBelongsDate(userId, data.BelongsDate);
            if (!data.UserCheckInfoId.NumberGtZero()) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            var r = _userCheckDetail.Add(data);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }
        
        [HttpPost, Route("Update")]
        public ApiResult Update([FromHeader(Name = "Authorization")] string auth, [FromBody] AddUserCheckDetailDto data)
        {
            if (data == null
                || !data.Id.NumberGtZero()
                || !data.WorkerTypeId.NumberGtZero())
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (data.BelongsDate > DateTime.Now.AddDays(1) || data.BelongsDate <= _config.MinDate)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_workerTypeInfo.ExistById(data.WorkerTypeId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_ID_NOT_EXIST);
            }

            data.UserCheckInfoId = _userCheckInfo.GetIdByUserIdAndBelongsDate(userId, data.BelongsDate);
            if (!data.UserCheckInfoId.NumberGtZero())
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            var r = _userCheckDetail.Update(data);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost,Route("Delete")]
        public ApiResult Delete(UserCheckDetailId data) 
        {
            if (data == null || !data.Id.NumberGtZero()) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_userCheckDetail.HasExistId(data.Id)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            var r = _userCheckDetail.Delete(data.Id);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpGet,Route("QueryAll")]
        public ApiResult QueryAll([FromHeader(Name = "Authorization")] string token, DateTime belongsDate)
        {
            if (!_user.GetUserIdByToken(token.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            var data = _userCheckDetail.QueryByUserIdAndBelongsDate(userId, belongsDate);
            return ApiResultStatus.Success(data);
        }
    }
}
